package com.it.a11threadsafe3;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class MyThread extends Thread {
    static int ticket = 0;
    static Lock lock = new ReentrantLock();
    public void run(){
        while(true){
            lock.lock();
            try {
                if(ticket == 1000){
                    break;
                }else{
                    Thread.sleep(1);
                    ticket++;
                    System.out.println(getName()+"卖了"+ticket+"张票!!!");
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                lock.unlock();
            }
        }
    }
}
